package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.Consult;
import com.example.springboot.entity.Destination;
import com.example.springboot.service.ConsultService;
import com.example.springboot.service.DestinationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/consult")
public class ConsultController {

    @Autowired
    private ConsultService consultService;

    @Autowired
    private DestinationService destinationService;

    /**
     * 获取咨询配置信息
     */
    @GetMapping("/config")
    public Result getConsultConfig() {
        Map<String, String> config = new HashMap<>();
        config.put("contactPhone", "028-12345678");
        config.put("wechat", "travel_service");
        config.put("email", "contact@tourism.com");
        config.put("workTime", "周一至周五 9:00-18:00");
        return Result.ok(config);
    }

    /**
     * 获取咨询目的地选项
     */
    @GetMapping("/destinations")
    public Result getConsultDestinations() {
        try {
            // 调用修改后的方法获取活跃目的地
            List<Destination> destinations = destinationService.getActiveDestinations();
            return Result.ok(destinations);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取目的地列表失败");
        }
    }

    /**
     * 提交咨询预约
     */
    @PostMapping("/submit")
    public Result submitConsult(@RequestBody Consult consult) {
        try {
            // 设置初始状态
            consult.setStatus(0); // 0-未处理
            boolean saved = consultService.save(consult);
            if (saved) {
                return Result.ok("预约提交成功，我们会尽快与您联系！");
            } else {
                return Result.error("提交失败，请稍后重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常，请稍后重试");
        }
    }
}